package com.example.administrator.zxygradute;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import java.lang.reflect.Method;

/**
 * Created by Darren on 2016/9/6.
 */
//沉浸式状态栏设置
public class ImmersiveStatusBarUtil {

    // 5.0 一下 使用这个4.4 -  5.0
    public static  void initStateViewLow(Activity context,int color) {
        Window window = context.getWindow();
        ViewGroup mContentView = (ViewGroup) context.findViewById(Window.ID_ANDROID_CONTENT);
        // First translucent status bar.
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int statusBarHeight = getStatusBarHeight(context);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
            // 如果已经为 ChildView 设置过了 marginTop, 再次调用时直接跳过
            if (lp != null && lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
                // 不预留系统空间
                ViewCompat.setFitsSystemWindows(mChildView, false);
                lp.topMargin += statusBarHeight;
                mChildView.setLayoutParams(lp);
            }
        }

        View statusBarView = mContentView.getChildAt(0);
        if (statusBarView != null && statusBarView.getLayoutParams() != null
                && statusBarView.getLayoutParams().height == statusBarHeight) {
            // 避免重复调用时多次添加 View
            statusBarView.setBackgroundColor(color);
            return;
        }
        statusBarView = new View(context);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusBarView.setBackgroundColor(color);
        // 向 ContentView 中添加假 View
        mContentView.addView(statusBarView, 0, lp);
    }
    // 获取任务栏高度
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     *  适用 5.0 以上的设备
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void initStateView(Activity context ,int color) {
        Window window = context.getWindow();
//取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏颜色
        window.setStatusBarColor(color);
        ViewGroup mContentView = (ViewGroup) context.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }
    }

    public static void initStateBarColor(Activity context,int color){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&Build.VERSION.SDK_INT<=Build.VERSION_CODES.LOLLIPOP){
            ImmersiveStatusBarUtil.initStateViewLow(context, color);

        }else if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP&& Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
           ImmersiveStatusBarUtil.initStateView(context,color);
        }else if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){

            //6.0沉浸效果待定
//            if(true)
//                return;

            //透明状态栏
                Window window = context.getWindow();
                window.setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


            // 部分机型的statusbar会有半透明的黑色背景
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(Color.TRANSPARENT);// SDK21
            }
        }
    }
    public static  int getNavigationBarHeight(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        int navigationbarHeight = 0;
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
            Resources resources = context.getResources();
            if (hasNavigationBar) {
                int resIdNavigationBar = resources.getIdentifier("navigation_bar_height", "dimen", "android");
                if (resIdNavigationBar > 0) {
                    navigationbarHeight = resources.getDimensionPixelSize(resIdNavigationBar);//navigationBar高度
                }
            }
        } catch (Exception e) {

        }

        return  navigationbarHeight;
    }
}
